package yAnalyser

import (
	"fmt"
	"go.uber.org/zap/zapcore"
	"reflect"
)

/**
* 实体类 YAnalyser
 */
type YAnalyser struct {
	AnalyserNum   *string  `json:"analyser_num"`
	CrushingPlant *string  `json:"crushing_plant"`
	Flux          *float64 `json:"flux"`
	Load          *float64 `json:"load"`
	Mat1          *float64 `json:"mat1"` //Cao
	Mat2          *float64 `json:"mat2"`
	Mat3          *float64 `json:"mat3"`
	Mat4          *float64 `json:"mat4"`
	Mat5          *float64 `json:"mat5"`
	Mat6          *float64 `json:"mat6"`
	Mat7          *float64 `json:"mat7"`
	Mat8          *float64 `json:"mat8"`
	Mat9          *float64 `json:"mat9"`  //保留
	Mat10         *float64 `json:"mat10"` //保留
	Mat11         *float64 `json:"mat11"` //保留
	Mat12         *float64 `json:"mat12"` //保留
	Mat13         *float64 `json:"mat13"` //保留
	Mat14         *float64 `json:"mat14"` //保留
	Mat15         *float64 `json:"mat15"` //保留
	Mat16         *float64 `json:"mat16"` //保留
	Speed         *float64 `json:"speed"`
	Status        *int     `json:"status"`
	TestAt        *int     `json:"test_at"`
	Token         *string  `gorm:"primaryKey" json:"token"`
}

func (this YAnalyser) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
	if this.Flux != nil {
		encoder.AddFloat64("flux", *this.Flux)
	}
	if this.Load != nil {
		encoder.AddFloat64("load", *this.Load)
	}
	// 对于每一个MatX和剩余的指针类型字段重复类似的检查和编码操作
	if this.AnalyserNum != nil {
		encoder.AddString("name", *this.AnalyserNum)
	}
	if this.Speed != nil {
		encoder.AddFloat64("speed", *this.Speed)
	}
	if this.Token != nil {
		encoder.AddString("token", *this.Token)
	}
	if this.TestAt != nil {
		encoder.AddInt("utc", *this.TestAt)
	}

	// 添加Mat1至Mat16的编码逻辑
	for i := 1; i <= 16; i++ {
		matField := fmt.Sprintf("mat%d", i)
		if matPtr := getFieldByName(this, matField); matPtr != nil {
			encoder.AddFloat64(matField, *matPtr.(*float64))
		}
	}

	return nil
}

// Helper function to dynamically access struct fields by name.
func getFieldByName(obj interface{}, name string) interface{} {
	v := reflect.ValueOf(obj).Elem()
	field := v.FieldByName(name)
	if !field.IsValid() {
		return nil
	}
	return field.Interface()
}
